<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="rolesList" stripe>
        <el-table-column label="展开" type="expand">
          <template v-slot="{row}">
            <el-row v-for="item1 in row.children" :key="item1.id" style="background:#cdcdcd;padding:20px;">
              <el-col :span="4" style="display:flex;align-items:center;height:100%;margin-top:100px;">
                <el-tag closable @close="removeById(row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <el-row v-for="item2 in item1.children" :key="item2.id"
                  style="background:#fff;margin:20px 0;padding:20px 0;">
                  <el-col :span="4" style="padding-top:20px">
                    <el-tag type="warning" closable @close="removeById(row,item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="20">
                    <el-tag type="danger" v-for="item3 in item2.children" :key="item3.id" closable
                      @close="removeById(row,item3.id)">
                      {{item3.authName}}</el-tag>

                  </el-col>
                </el-row>

              </el-col>
            </el-row>

          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-tooltip effect="dark" content="分配权限" placement="top" :enterable="false">
              <el-button type="warning" size="mini" icon="el-icon-setting" @click="showrolseDialog(row)">分配权限
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="权限分配" :visible.sync="rolseDialogVisible" width="50%" @close=setRightClose>
      <el-tree :data="rightList" :props="treeProps" node-key="id" show-checkbox default-expand-all
        :default-checked-keys="defkeys" ref="treeRef"></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="rolseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rolesList: [],
      rolseDialogVisible: false,
      rightList: [],
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      defkeys: [],
      roleId: ''
    }
  },
  methods: {
    async saveRights () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idStr = keys.join(',')
      console.log(idStr)
      // console.log(this.roleId)
      await this.$http.post(`roles/${this.roleId}/rights`, { rids: idStr })
      this.rolseDialogVisible = false
      this.getRolesList()
    },
    setRightClose () {
      this.defkeys = []
    },
    async showrolseDialog (row) {
      this.roleId = row.id
      const { data: res } = await this.$http.get('rights/tree')
      this.rightList = res.data
      const arr = []
      function fn (obj) {
        // console.log(obj.children.length)
        if (obj.children) {
          obj.children.forEach(item => {
            fn(item)
          })
        } else {
          arr.push(obj.id)
        }
      }
      fn(row)
      this.defkeys = arr
      // console.log(this.defkeys)
      // console.log(arr)
      // console.log(this.rightList)
      this.rolseDialogVisible = true
      // console.log(row)
    },
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      // console.log(res)
      this.rolesList = res.data
      // console.log(this.rolesList)
    },
    removeById (row, id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${row.id}/rights/${id}`)
        // console.log(res.meta)
        // console.log(res)
        if (res.meta.status !== 200) return this.$Message.error('删除失败')
        this.$Message.success('删除成功')
        // this.getRolesList()
        row.children = res.data // 重新渲染会重新刷新页面
      }).catch(() => {
        this.$Message({
          type: 'info',
          message: '已取消删除'

        })
      })
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getRolesList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.el-tag {
  margin: 10px 10px;
}
</style>
